/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package userinterface;

import com.sun.imageio.plugins.jpeg.JPEG;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import starwarslife.SWLController;
import starwarslife.SWLModel;
import starwarslife.StarWarsLife;

/**
 *
 * @author Yann
 */
public class GameFrame extends javax.swing.JFrame implements StarWarsLifeUI {

    /**
     * Creates new form GameFrame
     */
    private static final int MIN_SIZE = 25;
    private static final int MAX_SIZE = 100;
    private static final Dimension FRAME_DIMENTION = new Dimension(800, 650);
    private static final Dimension BOARD_PANEL_DIMENTION = new Dimension(600, 600);
    
    private int[][] bs;
    private int size;
    private SWLController ctrl;
    private JPanel[] gridPanel;
    

    public GameFrame(SWLController ctrl) {
        initComponents();
        this.ctrl = ctrl;
        cbGridSize.removeAllItems();
        for(int i = (int) Math.sqrt(MIN_SIZE) ; i<= Math.sqrt(MAX_SIZE); i++){
            cbGridSize.addItem(""+(int)Math.pow(i, 2));
            
        }
        this.setSize(FRAME_DIMENTION);
        boardPanel.setSize(BOARD_PANEL_DIMENTION);
        this.setVisible(true);
        btReset.setEnabled(false);
        
    }
    
    public void setBoardState(int[][] bs){
       this.bs = bs;
       displayBoardState();
    }
    
    public int[][] getBoardState(){
        return bs;
    }
    
    private void displayBoardState(){
        for(int i =0; i<bs.length; i++){
            for(int j = 0; j<bs[i].length; j++){
                if(bs[i][j] > 0) gridPanel[i*bs.length+j].setBackground(Color.GRAY);
                else gridPanel[i*bs.length+j].setBackground(Color.LIGHT_GRAY);
            }                        
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        boardPanel = new javax.swing.JPanel();
        controlPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbGridSize = new javax.swing.JComboBox();
        btGenerateGrid = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        btStartStop = new javax.swing.JToggleButton();
        btReset = new javax.swing.JButton();
        lbError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        boardPanel.setMaximumSize(new java.awt.Dimension(600, 600));
        boardPanel.setMinimumSize(new java.awt.Dimension(600, 600));
        boardPanel.setPreferredSize(new java.awt.Dimension(600, 600));
        boardPanel.setLayout(new java.awt.GridLayout(1, 0));

        controlPanel.setMaximumSize(new java.awt.Dimension(200, 600));
        controlPanel.setMinimumSize(new java.awt.Dimension(200, 600));

        jLabel1.setText("Game Settings:");

        jLabel2.setText("Grid size :");

        cbGridSize.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbGridSize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbGridSizeActionPerformed(evt);
            }
        });

        btGenerateGrid.setText("Generate Grid!");
        btGenerateGrid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btGenerateGridActionPerformed(evt);
            }
        });

        jLabel3.setText("Game mode :");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btStartStop.setText("Play!");
        btStartStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btStartStopActionPerformed(evt);
            }
        });

        btReset.setText("Reset");
        btReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btResetActionPerformed(evt);
            }
        });

        lbError.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        lbError.setForeground(new java.awt.Color(204, 0, 0));

        org.jdesktop.layout.GroupLayout controlPanelLayout = new org.jdesktop.layout.GroupLayout(controlPanel);
        controlPanel.setLayout(controlPanelLayout);
        controlPanelLayout.setHorizontalGroup(
            controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(controlPanelLayout.createSequentialGroup()
                .add(controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(controlPanelLayout.createSequentialGroup()
                        .add(31, 31, 31)
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 125, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(controlPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, controlPanelLayout.createSequentialGroup()
                .add(controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(lbError, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(controlPanelLayout.createSequentialGroup()
                        .add(btReset)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(btStartStop))
                    .add(controlPanelLayout.createSequentialGroup()
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(btGenerateGrid))
                    .add(controlPanelLayout.createSequentialGroup()
                        .add(25, 25, 25)
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(cbGridSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(49, 49, 49))
        );
        controlPanelLayout.setVerticalGroup(
            controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(controlPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(38, 38, 38)
                .add(controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(cbGridSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(btGenerateGrid)
                .add(28, 28, 28)
                .add(controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(31, 31, 31)
                .add(controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btStartStop)
                    .add(btReset))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lbError, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(boardPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(controlPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 191, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(controlPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(boardPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbGridSizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbGridSizeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbGridSizeActionPerformed

    private void btGenerateGridActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btGenerateGridActionPerformed
        boardPanel.removeAll();
        lbError.setText("");
        size = Integer.parseInt(cbGridSize.getSelectedItem().toString());
        gridPanel = new JPanel[size];
        int length = (int) Math.sqrt(size);
        bs = new int[length][length];
        int panelSize = 600/length;
        boardPanel.setLayout(new GridLayout(length, length));
        boardPanel.setBorder(new LineBorder(Color.gray));
        for (int i = 0; i < size; i++) {
            gridPanel[i] = new JPanel();
            gridPanel[i].setSize(panelSize, panelSize);
            gridPanel[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            boardPanel.add(gridPanel[i], i);
        }
    }//GEN-LAST:event_btGenerateGridActionPerformed

    private void btStartStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btStartStopActionPerformed
        if(gridPanel == null){
            lbError.setText("Please generate grid.");
            btStartStop.setSelected(false);
        }        
        else if(btStartStop.isSelected()){
            btStartStop.setText("Pause");
            ctrl.start();
            btReset.setEnabled(false);
        }
        else{
            btStartStop.setText("Play!");
            ctrl.pause();
            btReset.setEnabled(true);
        }
    }//GEN-LAST:event_btStartStopActionPerformed

    private void btResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btResetActionPerformed
        ctrl.reset();
    }//GEN-LAST:event_btResetActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel boardPanel;
    private javax.swing.JButton btGenerateGrid;
    private javax.swing.JButton btReset;
    private javax.swing.JToggleButton btStartStop;
    private javax.swing.JComboBox cbGridSize;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lbError;
    // End of variables declaration//GEN-END:variables
}
